<?php
namespace app\admin\controller;
use think\Controller;
class Getuserinfo extends Controller {
    /**
     * 第一步，微信授权登录，获取code
     *
     */
    public function getBaseInfo(){
    //1.获取code
//        ①
//            $appId = "wx6a55c96c1a56a167";
//            $url_redirect =urldecode("http://119.23.239.149/admin/Getuserinfo/getUserOpenId");
//            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$url_redirect."&response_type=code&scope=SCOPE&state=STATE#wechat_redirect ";
//            $this->redirect($url);
        $config = ['redirect_uri'=>'http://47.93.184.172/admin/Getuserinfo/getUserOpenId'];
        $weixinAuth = new Weixinauth($config);
        $weixinAuth->code();
    }

    /**
     * @return mixed
     * 第二步，获取网页授权access_token
     */
    public function getUserOpenId()
    {
//        $code = request()->get('code','');
//        $url = " https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
//        $res = $this->request($url);
//        dump($res);
//        return $this->fetch('getuseropenid');
        $m=new Weixinauth();
        $userInfo = $m->getUserInfo();
         print_r($userInfo);


    }


    //https请求
    public function request($url, $data = null,$time_out=60,$out_level="s",$headers=array())
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        if($out_level=="s")
        {
            //超时以秒设置
            curl_setopt($curl, CURLOPT_TIMEOUT,$time_out);//设置超时时间
        }elseif ($out_level=="ms")
        {
            curl_setopt($curl, CURLOPT_TIMEOUT_MS,$time_out);  //超时毫秒，curl 7.16.2中被加入。从PHP 5.2.3起可使用
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        if($headers)
        {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//如果有header头 就发送header头信息
        }
        $res = curl_exec($curl);
        $res = json_decode($res, true);
        curl_close($curl);
        if (!empty($res)) {
            if (isset($res['errcode']) && $res['errcode']) {
                $this->_error_number = $res['errcode'];
                $this->_error = $res['errmsg'];
                return false;
            }
            return $res;
        }
    }

}
